package comment.dao.impl;

import comment.dao.CommentDao;
import comment.entity.CommentVO;
import generic.dao.impl.GenericDaoImpl;
import generic.page.Pages;

import java.util.List;

public class CommentDaoImpl extends GenericDaoImpl<CommentVO, Integer> implements CommentDao {

    @Override
    public CommentVO findCommentVOById(Integer commentId) {
        return (CommentVO) this.findById(CommentVO.class, commentId);
    }


    @Override
    public void deleteCommentById(Integer commentId) {
        this.delete(this.findCommentVOById(commentId));
    }

    @Override
    public CommentVO addCommentVO(CommentVO commentVO) {
        this.save(commentVO);
        return commentVO;
    }

    @Override
    public Pages queryPageByArticle(Pages pages, Integer articleId) {
        StringBuffer hql = new StringBuffer();
        hql.append("select count(*) from CommentVO a where a.ArticleVO.id=").append(articleId);
        pages.setRowCount(Integer.parseInt(this.getSession().createQuery(hql.toString()).uniqueResult().toString()));

        Pages.init(pages);

        //查询记录
        hql.delete(0, 15);
        List list = this.getSession().createQuery(hql.toString())
                .setFirstResult(pages.getOffset())
                .setMaxResults(pages.getPageSize()).list();
        pages.setList(list);
        return pages;
    }

    @Override
    public Pages queryPageByBlogData(Pages pages, Integer blogDataId) {
        StringBuffer hql = new StringBuffer();
        hql.append("select count(*) from CommentVO c where c.blogDataVO.id=").append(blogDataId);
        Integer s = Integer.parseInt(this.getSession().createQuery(hql.toString()).uniqueResult().toString());

        pages.setRowCount(Integer.parseInt(this.getSession().createQuery(hql.toString()).uniqueResult().toString()));

        Pages.init(pages);

        //查询记录
        hql.delete(0, 15);
        List list = this.getSession().createQuery(hql.toString())
                .setFirstResult(pages.getOffset())
                .setMaxResults(pages.getPageSize()).list();
        pages.setList(list);
        return pages;
    }


}
